package hd.util.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

/**
 * 菊花转
 * <p/>
 * Created by haidy on 14/12/14.
 */
public class IndicatorView extends View {


    //是否在加载
    private boolean loading = false;

    //当前旋转的角度
    private int degree = 0;

    //旋转的角度
    private final int rotateDegree = 30;
    //颜色渐变的比例
    private final float ratio = 0.06666667f;
    private Paint paint;

    public IndicatorView(Context context) {
        this(context, null);
    }

    public IndicatorView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public IndicatorView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        // TODO 初始化
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.GRAY);
        paint.setStyle(Paint.Style.FILL);
        stopLoading();
    }

    /**
     * 开始加载
     */
    public void startLoading() {
        if (!loading) {
            setVisibility(VISIBLE);
            loading = true;
            degree = 0;
            load();
        }
    }

    private void load() {
        postDelayed(new Runnable() {
            @Override
            public void run() {
                degree += rotateDegree;
                degree = (degree == 360 ? 0 : degree);
                invalidate();
                if (loading)
                    load();
            }
        }, 80);
    }

    /**
     * 停止加载
     */
    public void stopLoading() {
        setVisibility(GONE);
        loading = false;
    }

    /**
     * 设置菊花的颜色
     *
     * @param color 颜色
     */
    public void setColor(int color) {
        paint.setColor(color);
    }

    /**
     * 获取菊花的颜色
     *
     * @return 颜色
     */
    public int getColor() {
        return paint.getColor();
    }

    /**
     * 是否在加载中
     *
     * @return loading
     */
    public boolean isLoading() {
        return loading;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // TODO 绘画一个菊花
        int width = getWidth();
        int height = getHeight();
        float radius = (width > height ? height : width) / 2f;
//        System.out.println(radius);
        //将远点移到View中心
        canvas.translate(width / 2, height / 2);
        canvas.rotate(degree);
        float leafWidth = radius / 10f;//花瓣宽度
        canvas.rotate(180);
        for (int i = 1; i <= 12; i++) {//绘制12个花瓣
            Paint p = new Paint(paint);
            p.setAlpha((int) (255 * i * ratio));
            RectF rectF = new RectF();
            rectF.left = -leafWidth / 2f;
            rectF.right = leafWidth / 2;
            rectF.top = radius / 2;
            rectF.bottom = radius;
            canvas.save();
            canvas.rotate(i * 30);//将现在坐标系正向旋转 i*30°
            canvas.drawRoundRect(rectF, leafWidth / 2, leafWidth / 2, p);
            canvas.restore();
        }
    }
}
